package me.passtime.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import me.passtime.projt.Home;
import me.passtime.projt.Main;
import me.passtime.projt.R;
import me.passtime.projt.util.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 下午3:41
 */
public class QueryCurrentMonthPage extends Activity implements View.OnClickListener {
    private TextView mPhoneNumber;
    private TextView mCurrentDate;
    private TextView mMonthFee;
    private Button mUserInfo;
    private SimpleAdapter listAdapter;
    private ListView mListView;
    private List<HashMap<String, String>> listDate = new ArrayList<HashMap<String, String>>();
    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 8;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_month_query);

        mHandler = new Handler();
        mController=new QueryController(this);
        initData();
        queryPhoneAmount();
        this.findViewById(R.id.query_month_back_btn).setOnClickListener(this);
        this.findViewById(R.id.query_month_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.query_month_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.query_month_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.query_month_exit).setOnClickListener(this);
    }

    private void initData() {
        mUserInfo = (Button) this.findViewById(R.id.query_month_person_btn);
        mUserInfo.setText(UserHolder.phoneNumber + "\r\n" + UserHolder.userName);
        mPhoneNumber = (TextView) this.findViewById(R.id.query_month_phone_number);
        mPhoneNumber.setText(UserHolder.phoneNumber);
        mCurrentDate = (TextView) this.findViewById(R.id.query_month_date);
        mCurrentDate.setText(DateUtils.formatCurrentDate());
        mMonthFee = (TextView) this.findViewById(R.id.query_month_fee);
        mListView = (ListView) this.findViewById(R.id.query_month_list_view);

        listAdapter = new SimpleAdapter(this, listDate, R.layout.month_list_view_item,
                new String[]{"itemTitle", "itemText"},
                new int[]{R.id.item_name, R.id.item_value});
        mListView.setAdapter(listAdapter);

    }

    private void queryPhoneAmount() {
        showDialog(DIALOG_PROGRESS);
        mController.queryCM(UserHolder.phoneNumber,DateUtils.formatDateToQueryYearMonth(new Date()));
    }
    public void updateCurrentMonthInfo(String amount,List<String> resultList){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mMonthFee.setText("226.5元");
            }
        });



//        for(String record : resultList){
//
//        }
        listDate.clear();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("itemTitle", "基本套餐费");
        map.put("itemText", "220.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "本地通话费");
        map.put("itemText", "120.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "增值业务费");
        map.put("itemText", "20.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "   3G上网费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        listAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.query_month_back_btn:
                PageJumpUtils.jump(this, Home.class);
                break;
            case R.id.query_month_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.query_month_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.query_month_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.query_month_exit:
                AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
